package com.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hotel.entity.RoomType;
import com.hotel.dto.RoomTypeDTO;
import com.hotel.vo.RoomTypeVO;
import com.hotel.mapper.RoomTypeMapper;
import com.hotel.service.RoomTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 房间类型服务实现类
 */
@Service
public class RoomTypeServiceImpl extends ServiceImpl<RoomTypeMapper, RoomType> implements RoomTypeService {
    
    @Autowired
    private RoomTypeMapper roomTypeMapper;
    
    @Override
    public Page<RoomTypeVO> getRoomTypePage(int current, int size, String keyword, String status) {
        Page<RoomType> page = new Page<>(current, size);
        
        LambdaQueryWrapper<RoomType> wrapper = new LambdaQueryWrapper<>();
        
        // 关键词搜索
        if (StringUtils.hasText(keyword)) {
            wrapper.and(w -> w.like(RoomType::getTypeName, keyword)
                    .or().like(RoomType::getTypeCode, keyword));
        }
        
        // 状态过滤
        if (StringUtils.hasText(status)) {
            wrapper.eq(RoomType::getStatus, "enabled".equals(status) ? 1 : 0);
        }
        
        wrapper.orderByAsc(RoomType::getTypeCode);
        
        Page<RoomType> roomTypePage = this.page(page, wrapper);
        
        // 转换为VO
        Page<RoomTypeVO> voPage = new Page<>();
        BeanUtils.copyProperties(roomTypePage, voPage);
        
        List<RoomTypeVO> voList = roomTypePage.getRecords().stream().map(roomType -> {
            RoomTypeVO vo = new RoomTypeVO();
            BeanUtils.copyProperties(roomType, vo);
            
            // 设置可用房间数量和总房间数量
            // 这里可以通过查询房间表获取，或者在Mapper中添加相应的方法
            // 简化处理，这里暂时不设置
            
            return vo;
        }).collect(Collectors.toList());
        
        voPage.setRecords(voList);
        return voPage;
    }
    
    @Override
    public RoomTypeVO getRoomTypeById(Long id) {
        RoomType roomType = this.getById(id);
        if (roomType == null) {
            return null;
        }
        
        RoomTypeVO vo = new RoomTypeVO();
        BeanUtils.copyProperties(roomType, vo);
        
        return vo;
    }
    
    @Override
    @Transactional
    public boolean createRoomType(RoomTypeDTO roomTypeDTO) {
        // 检查房型名称是否已存在
        RoomType existingRoomType = this.getRoomTypeByName(roomTypeDTO.getTypeName());
        if (existingRoomType != null) {
            return false;
        }
        
        RoomType roomType = new RoomType();
        BeanUtils.copyProperties(roomTypeDTO, roomType);
        
        // 设置默认状态为启用
        if (roomType.getStatus() == null) {
            roomType.setStatus(1);
        }
        
        roomType.setCreateTime(LocalDateTime.now());
        
        return this.save(roomType);
    }
    
    @Override
    @Transactional
    public boolean updateRoomType(Long id, RoomTypeDTO roomTypeDTO) {
        RoomType roomType = this.getById(id);
        if (roomType == null) {
            return false;
        }
        
        // 检查房型名称是否已被其他房型使用
        if (!roomType.getTypeName().equals(roomTypeDTO.getTypeName())) {
            RoomType existingRoomType = this.getRoomTypeByName(roomTypeDTO.getTypeName());
            if (existingRoomType != null) {
                return false;
            }
        }
        
        BeanUtils.copyProperties(roomTypeDTO, roomType);
        
        return this.updateById(roomType);
    }
    
    @Override
    @Transactional
    public boolean deleteRoomType(Long id) {
        // 检查房型是否被使用
        if (this.isRoomTypeInUse(id)) {
            return false;
        }
        
        return this.removeById(id);
    }
    
    @Override
    public RoomType getRoomTypeByName(String name) {
        LambdaQueryWrapper<RoomType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoomType::getTypeName, name);
        return this.getOne(wrapper);
    }
    
    @Override
    public List<RoomTypeVO> getEnabledRoomTypes() {
        LambdaQueryWrapper<RoomType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoomType::getStatus, 1);
        wrapper.orderByAsc(RoomType::getTypeCode);
        
        List<RoomType> roomTypes = this.list(wrapper);
        
        return roomTypes.stream().map(roomType -> {
            RoomTypeVO vo = new RoomTypeVO();
            BeanUtils.copyProperties(roomType, vo);
            return vo;
        }).collect(Collectors.toList());
    }
    
    @Override
    @Transactional
    public boolean updateRoomTypeStatus(Long id, String status) {
        RoomType roomType = this.getById(id);
        if (roomType == null) {
            return false;
        }
        
        roomType.setStatus("enabled".equals(status) ? 1 : 0);
        
        return this.updateById(roomType);
    }
    
    @Override
    public Map<String, Object> getRoomTypeStats() {
        return roomTypeMapper.getRoomTypeStats();
    }
    
    @Override
    public List<Map<String, Object>> getRoomTypeRoomCount() {
        return roomTypeMapper.getRoomTypeRoomCount();
    }
    
    @Override
    public boolean isRoomTypeInUse(Long roomTypeId) {
        int count = roomTypeMapper.countRoomsByRoomType(roomTypeId);
        return count > 0;
    }
    
    @Override
    public List<Map<String, Object>> getRoomTypePriceHistory(Long roomTypeId) {
        return roomTypeMapper.getRoomTypePriceHistory(roomTypeId);
    }
}